//
//  XRTabbarVC.m
//  LoanSimulat
//
//  Created by mc on 2017/11/1.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "XRTabbarVC.h"
#import "XRLoanVC.h"
#import "XRLousVC.h"
#import "XRMyVC.h"

@interface XRTabbarVC ()

@end

@implementation XRTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpTabViewControllers];
    //    [self clearTabBarShadow];
    
}
#pragma mark ---安装子视图控制器
- (void)setUpTabViewControllers
{
    
    UIColor *titleHighlightedColor = UIColor_xr_theme;
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleHighlightedColor, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];
    
    //首页
    XRLoanVC *LoanVC = [[XRLoanVC alloc] init];
    LoanVC.title = @"借款";
    UINavigationController *LoanNav = [[UINavigationController alloc]initWithRootViewController:LoanVC];
    [self addOneChildViewController:LoanNav WithTitle:@"借款"imageName:@"tabbar_homes_no" selectedImageName:@"tabbar_homes_select" BadgeAnimType:NO];
    
    
    //所有商品
    XRLousVC *LousVC = [[XRLousVC alloc] init];
    LousVC.title = @"白条";
    UINavigationController *LousNav = [[UINavigationController alloc]initWithRootViewController:LousVC];
    [self addOneChildViewController:LousNav WithTitle:@"白条" imageName:@"tabbar_good_no" selectedImageName:@"tabbar_good_select" BadgeAnimType:NO];
    

    //我的杏仁钱包
    XRMyVC *MyVC = [[XRMyVC alloc] init];
    MyVC.title = @"我的";
    UINavigationController *MyNav = [[UINavigationController alloc]initWithRootViewController:MyVC];
    [self addOneChildViewController:MyNav WithTitle:@"我的" imageName:@"tabbar_myInfos_no" selectedImageName:@"tabbar_myInfos_select" BadgeAnimType:NO];
    
}
#pragma mark ---添加一个子控制器
- (void)addOneChildViewController:(UIViewController *)viewController WithTitle:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName BadgeAnimType:(BOOL)addBadge
{
    
    viewController.tabBarItem.title = title;
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:UIColor_xr_theme} forState:UIControlStateSelected];
    
    viewController.tabBarItem.image = [UIImage imageNamed:imageName];
    UIImage *image = [UIImage imageNamed:selectedImageName];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = image;
    
    [self addChildViewController:viewController];
}

///去除TabBar自带的顶部阴影
- (void)clearTabBarShadow
{
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    //    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
    [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    [self dropShadowWithOffset:CGSizeMake(0, -1.5)
                        radius:1
                         color:[UIColor blackColor]
                       opacity:0.5];
    
}


- (void)dropShadowWithOffset:(CGSize)offset
                      radius:(CGFloat)radius
                       color:(UIColor *)color
                     opacity:(CGFloat)opacity {
    
    // Creating shadow path for better performance
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, self.tabBar.bounds);
    self.tabBar.layer.shadowPath = path;
    CGPathCloseSubpath(path);
    CGPathRelease(path);
    
    self.tabBar.layer.shadowColor = color.CGColor;
    self.tabBar.layer.shadowOffset = offset;
    self.tabBar.layer.shadowRadius = radius;
    self.tabBar.layer.shadowOpacity = opacity;
    
    // Default clipsToBounds is YES, will clip off the shadow, so we disable it.
    self.tabBar.clipsToBounds = NO;
}


@end
